package com.hmall.order.web;

import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.RequsetParams;
import com.hmall.order.service.IOrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private IOrderService orderService;

   @Autowired(required = false)
   private RabbitTemplate rabbitTemplate;


   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   /**
    * 下单业务
    * @param requestParams
    * @return
    */
   @PostMapping
   public Long  add(@RequestBody RequsetParams requestParams){

      Long orderId = orderService.createOrder(requestParams);
      //发送订单信息到死信队列
      rabbitTemplate.convertAndSend("order.exchange","order.key",orderId);

      return new Long(orderId);
   }
}
